﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CFR.Silverlight.Scheduling
{
    public class EventItem : ContentControl
    {

        private ViewBase _view;

        public ViewBase View
        {
            get { return _view; }
            set { _view = value; }
        }

        public bool IsDragging { get; internal set; }
        private bool _isDraggable = true;

        public bool IsDraggable
        {
            get { return _isDraggable; }
            set { _isDraggable = value; }
        }


        public event MouseButtonEventHandler HandleLeftMouseButtonDown;
        protected internal void RaiseHandleLeftMouseButtonDown(MouseButtonEventArgs e)
        {
            if (HandleLeftMouseButtonDown != null)
                HandleLeftMouseButtonDown(this, e);
        }

        #region Event (DependencyProperty)

        /// <summary>
        /// Gets or sets the event
        /// </summary>
        public Event Event
        {
            get { return (Event)GetValue(EventProperty); }
            set { SetValue(EventProperty, value); DataContext = value; }
        }
        public static readonly DependencyProperty EventProperty =
            DependencyProperty.Register("Event", typeof(Event), typeof(EventItem),
              new PropertyMetadata(null));

        #endregion
        #region Title (DependencyProperty)

        /// <summary>
        /// Gets and sets the title to show
        /// </summary>
        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(EventItem),
              new PropertyMetadata(null));

        #endregion

        #region TitleContentTemplate (DependencyProperty)

        /// <summary>
        /// Gets the title area's content template.
        /// </summary>
        internal DataTemplate TitleContentTemplate
        {
            get { return (DataTemplate)GetValue(TitleContentTemplateProperty); }
            set { SetValue(TitleContentTemplateProperty, value); }
        }
        public static readonly DependencyProperty TitleContentTemplateProperty =
            DependencyProperty.Register("TitleContentTemplate", typeof(DataTemplate), typeof(EventItem),
              new PropertyMetadata(null));

        #endregion


        protected internal Border HandleBorder { get; private set; }
        protected internal HyperlinkButton TitleHyperlink { get; private set; }
        protected internal FrameworkElement StretcherElement { get; private set; }

        public EventItem(ViewBase view) 
        {
            View = view;
            this.DefaultStyleKey = typeof(EventItem);
            this.UseLayoutRounding = false;

            SetupMouseEvents();

        }


        public EventItem()
        {
            this.DefaultStyleKey = typeof(EventItem);
            this.UseLayoutRounding = false;

            SetupMouseEvents();


        }



        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            HandleBorder = (Border)GetTemplateChild("HandleBorder");
            HandleBorder.Visibility = (Event.IsAllDayEvent) ? Visibility.Collapsed : Visibility.Visible;
            HandleBorder.MouseLeftButtonDown += new MouseButtonEventHandler(HandleBorder_MouseLeftButtonDown);

            TitleHyperlink = (HyperlinkButton)GetTemplateChild("TitleHyperlink");
            TitleHyperlink.Click += new RoutedEventHandler(TitleHyperlink_Click);

            StretcherElement = (FrameworkElement)GetTemplateChild("StretcherElement");
            StretcherElement.MouseLeftButtonDown += new MouseButtonEventHandler(StretcherElement_MouseLeftButtonDown);
            StretcherElement.MouseLeftButtonUp += new MouseButtonEventHandler(StretcherElement_MouseLeftButtonUp);
        }

        void StretcherElement_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            IsDragging = false;
        }

        void StretcherElement_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (IsDraggable)
                IsDragging = true;
        }

        void HandleBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;

            RaiseHandleLeftMouseButtonDown(e);
        }

        void TitleHyperlink_Click(object sender, RoutedEventArgs e)
        {
            if(!IsDragging)
                View.Scheduler.RaiseEventHyperlinkClicked(new EventItemEventArgs(this));
        }
   
        private void SetupMouseEvents()
        {
            this.MouseLeftButtonUp += new MouseButtonEventHandler(EventItem_MouseLeftButtonUp);
            this.MouseRightButtonDown += new MouseButtonEventHandler(EventItem_MouseRightButtonDown);
            this.MouseMove += new MouseEventHandler(EventItem_MouseMove);
            this.MouseLeave += new MouseEventHandler(EventItem_MouseLeave);
        }

        void EventItem_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {

            if (!IsDragging)
            {
                e.Handled = true;
                View.Scheduler.RaiseEventItemRightClicked(new EventItemEventArgs(this));

            }
        }

        void EventItem_MouseLeave(object sender, MouseEventArgs e)
        {
            if(!IsDragging && IsDraggable)
                StretcherElement.Visibility = System.Windows.Visibility.Collapsed;
        }
        void EventItem_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (!IsDragging && IsDraggable)
                View.Scheduler.RaiseEventItemClicked(new EventItemEventArgs(this));

            e.Handled = true;
        }

        void EventItem_MouseMove(object sender, MouseEventArgs e)
        {
            if (!IsDragging && IsDraggable)
                StretcherElement.Visibility = (e.GetPosition(this).Y + 10 > this.ActualHeight && !Event.IsAllDayEvent) ? Visibility.Visible : Visibility.Collapsed;
        }
    }
}
